﻿using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.PB;
using Coldairarrow.Util;
using EFCore.Sharding;
using LinqKit;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.PB
{
    public partial class PB_DailyInventoryStatisticsBusiness : BaseBusiness<PB_DailyInventoryStatistics>, IPB_DailyInventoryStatisticsBusiness, ITransientDependency
    {
        public PB_DailyInventoryStatisticsBusiness(IDbAccessor db)
            : base(db)
        {
        }

        #region 外部接口

        public async Task<PageResult<PB_DailyInventoryStatistics>> GetDataListAsync(PageInput<ConditionDTO> input)
        {
            var q = GetIQueryable();
            var where = LinqHelper.True<PB_DailyInventoryStatistics>();
            var search = input.Search;

            //筛选
            if (!search.Condition.IsNullOrEmpty() && !search.Keyword.IsNullOrEmpty())
            {
                var newWhere = DynamicExpressionParser.ParseLambda<PB_DailyInventoryStatistics, bool>(
                    ParsingConfig.Default, false, $@"{search.Condition}.Contains(@0)", search.Keyword);
                where = where.And(newWhere);
            }

            return await q.Where(where).GetPageResultAsync(input);
        }

        public async Task<PB_DailyInventoryStatistics> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }

        public async Task AddDataAsync(PB_DailyInventoryStatistics data)
        {
            await InsertAsync(data);
        }

        public async Task UpdateDataAsync(PB_DailyInventoryStatistics data)
        {
            await UpdateAsync(data);
        }

        public async Task DeleteDataAsync(List<string> ids)
        {
            await DeleteAsync(ids);
        }

        public List<PB_DailyInventoryStatisticsDTO> GetDataDTOAsync()
        {
            return Db.GetListBySql<PB_DailyInventoryStatisticsDTO>(@"select `PB_Material`.`Code` AS `Code`,`PB_Material`.`Name` AS `Name`,`PB_Material`.`Spec` AS `Spec`,`PB_Location`.`Name` AS `hwmc`,`PB_Location`.`Code` AS `hwbm`,`Base_Department`.`Name` AS `ckmc`,`Base_Department`.`Id` AS `ckid`,`Base_UnitItems`.`Id` AS `jldw`,sum(`PB_Stock`.`StockNumber`) AS `StockNumber` from ((((`PB_Stock` join `PB_Location` on((`PB_Stock`.`LocationId` = `PB_Location`.`Id`))) join `PB_Material` on((`PB_Stock`.`MaterialId` = `PB_Material`.`Id`))) join `Base_Department` on((`PB_Location`.`StorId` = `Base_Department`.`Id`))) join `Base_UnitItems` on((`PB_Material`.`storageId` = `Base_UnitItems`.`Id`))) group by `PB_Material`.`Code`,`PB_Material`.`Name`,`PB_Material`.`Spec`,`PB_Location`.`Name`,`PB_Location`.`Code`,`Base_Department`.`Name`,`Base_Department`.`Id`,`Base_UnitItems`.`Id` ");
        }

        #endregion

        #region 私有成员

        #endregion
    }
}